import librosa
import time
import json
import pygame
import sys
import numpy as np

def tts_and_play_audio():
    tmp_audio_path = sys.argv[1]
    pygame.mixer.init()
    pygame.mixer.music.load(tmp_audio_path)
    pygame.mixer.music.set_volume(0.8)

    x, sr = librosa.load(tmp_audio_path, sr=8000)

    x = x - min(x)
    x = x / max(x)
    x = np.log(x) + 1
    x = x / max(x) * 1.2

    list = []
    pygame.mixer.music.play()
    s_time = time.time()
    try:
        for _ in range(int(len(x) / 800)):
            it = x[int((time.time() - s_time) * 8000) + 1]
            if it < 0:
                it = 0

            list.append(round(float(it),2))
            time.sleep(0.1)
    except:
        pass

    time.sleep(0.1)
    list.append(0)
    print(json.dumps(list))
    return json.dumps(list)

if __name__ == '__main__':
    tts_and_play_audio()